Skip to main content

Sum of a Single Vector

This is an example of computing the sum of a single vector input.

from csdl_om import Simulatorfrom csdl import Modelimport csdlimport numpy as np

class ExampleSingleVector(Model):
    def define(self):        n = 3
        # Declare a vector of length 3 as input        v1 = self.declare_variable('v1', val=np.arange(n))
        # Output the sum of all the elements of the vector v1        self.register_output('single_vector_sum', csdl.sum(v1))

sim = Simulator(ExampleSingleVector())sim.run()
print('v1', sim['v1'].shape)print(sim['v1'])print('single_vector_sum', sim['single_vector_sum'].shape)print(sim['single_vector_sum'])
[0. 1. 2.]single_vector_sum (1,)[3.]